﻿using Comm;
using Kinlo.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo.MySql
{
    public class CodeFirstRep
    {
        string strConn;
        public CodeFirstRep()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["WeightDBMySql"].ConnectionString;

        }
        public int DeleteCodeFirst(CodeFirst w)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {

                    var cmd = string.Format(@"DELETE FROM Code_First WHERE Code='{0}'", w.Code);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "DeleteCodeFirst", ex);
                return 0;
            }
        }

        public int Add2(CodeFirst w)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = @"INSERT Code_First  (ID, CodeIndex, Code, ProductDate ,IsValidate)
                        VALUES  ( @ID,@CodeIndex, @Code, @ProductDate ,@IsValidate)";

                    var sqlCmd = new MySqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new MySqlParameter("@ID", w.ID == null ? DBNull.Value : (object)w.ID));
                    sqlCmd.Parameters.Add(new MySqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new MySqlParameter("@CodeIndex", w.CodeIndex == null ? DBNull.Value : (object)w.CodeIndex));
                    sqlCmd.Parameters.Add(new MySqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new MySqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Add2", ex);
                return 0;
            }
        }

        public int Add(CodeFirst w)
        {
            try
            { 
                return 0;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Add", ex);
                return 0;
            }

        }

        /// <summary>
        /// 更新状态 是否已失效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Update(string code)
        {
            try
            { 
                return 0;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return 0;
            }
        }

        public CodeFirst GetCodeFirst(string code)
        {
            try
            { 
                return null;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return null;
            }

        }
        public IList<CodeFirst> GetCodeFirst1()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = @"SELECT  * FROM Code_First WHERE IsValidate=1 AND	ProductDate > DATE_ADD(NOW(),INTERVAL -7 DAY) ORDER BY ProductDate DESC LIMIT 1000";
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    var CodeFirst = sqlCmd.ExecuteReader().ReaderToList<CodeFirst>();
                    if (CodeFirst.Count > 0)
                        return CodeFirst;
                }
                return null;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return null;
            }
        }


        public List<CodeFirst> GetCodeFirst()
        { 
            return null;
        }

        /// <summary>
        /// 请除 day 天前的记录
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public int ClearRecordByDay(int day = 0)
        {
            try
            { 
                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "ClearRecordByDay", ex);
                return 0;
            }

        }

    }

}
